package io.mft.as.adapter.tester;

import java.util.Map;

import io.mft.as.adapter.base.BaseFunction;
import io.mft.as.adapter.exception.CommonException;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class FunctionTester extends BaseTest {
	private BaseFunction function;

	public void addField(String key, String value) {
		getRequest().addFieldValue(key, value);
	}

	public void addFields(Map<String, String> map) {
		for (String key : map.keySet()) {
			getRequest().addFieldValue(key, map.get(key));
		}
	}

	public FunctionTester(BaseFunction function) {
		super();
		this.function = function;
	}

	@Override
	public void testInvoke() {
		function.invoke(getContext());
		if (response.getErrorNo() != 0) {
			throw new CommonException(1, response.getData());
		} else {
			log.info(response.getData());
		}
	}
}
